<?php

namespace App\Department\Controllers;

use App\Extensions\Actions\Upload\PatentCertUploadAction;
use App\Repositories\PatentApplyRepository;
use App\Services\PatentApplyService;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use App\Department\Actions\Review\DeptReview;
use PhpOffice\PhpWord\IOFactory;

class PatentApplyController extends Controller
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '专利申请前登记审批';
    protected $service;
    public $repository;

    public function __construct(PatentApplyService $service, PatentApplyRepository $repository)
    {
        $this->service = $service;
        $this->repository = $repository;
    }

    public function index(Content $content)
    {
        $grid = $this->grid();
        $grid->model()->when(request()->get('is_review'), function ($query) {
            $query->where('dept_review_status', 0);
        });
        return $content
            ->title($this->title)
            ->body($grid);
    }

    public function review(Content $content)
    {
        $grid = $this->grid();
        $grid->model()->where('dept_review_status', 0);
        return $content
            ->title($this->title)
            ->description('待审批')
            ->body($grid);
    }

    protected function grid()
    {
        $user = Admin::user();
        $grid = $this->repository->deptGrid();
        $grid = $this->service->gridColumns($grid, 2, 0);
        $grid->hideColumns(['department.name']);
        //$grid->disableCreateButton();
        $grid->selector(function (Grid\Tools\Selector $selector) {
            $selector->selectOne('status', '状态', ['待审批', '进行中', '通过', '未通过'], function ($query, $value) {
                $query->when(in_array($value, [1, 2, 3]), function ($query) use ($value) {
                    $query->where('review_status', $value - 1);
                }, function ($query) {
                    $query->where('dept_review_status', 0);
                });
            });
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) use($user){
            $actions->disableView();
            $actions->disableDelete();
            if($actions->row->user_id != $user['id'] || $actions->row->review_status != 3){
                $actions->disableEdit();
            }
            if (!$actions->row->dept_review_status) {
                $actions->prepend(new DeptReview($actions));
            }
            if($actions->row->user_id == $user['id']){
                if ($actions->row->review_status == 1 && !$actions->row->cert) {
                    $actions->prepend(new PatentCertUploadAction($actions));
                }
            }
        });
        $grid->disableCreateButton();
        return $grid;
    }

    public function publish(Content $content)
    {
        $user = Admin::user();
        $grid = $this->repository->deptGrid();
        $grid = $this->service->gridColumns($grid, 2, 1);
        $grid->disableCreateButton();
        $grid->actions(function (Actions $actions)use($user) {
            $actions->disableView();
            $actions->disableDelete();
            $actions->disableEdit();
            if($user['id'] == $actions->row->user_id){
                if ($actions->row->review_status == 1 && !$actions->row->cert) {
                    $actions->prepend(new PatentCertUploadAction($actions));
                }
            }
        });
        return $content->title('已授权专利')->body($grid);
    }

    public function download($id)
    {
        $model = $this->repository->find($id);
        if (!$model) {
            return response()->view('errors.404');
        }
        return $this->service->downloadTable($model);
    }

    public function preview($id)
    {
        $model = $this->repository->find($id);
        $templateProcessor = $this->service->tableTemplateProcessor($model);
        $wordfile = IOFactory::load($templateProcessor->save());
        $xmlWriter = IOFactory::createWriter($wordfile, "HTML"); // 转换为html
        $html = $xmlWriter->getContent(); // html内容获取
        echo $html;
    }

    public function create(Content $content)
    {
        return $content
            ->title($this->title)
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    public function store()
    {
        return $this->form()->store();
    }

    public function edit($id, Content $content)
    {
        $model = $this->repository->findOrFail($id);
        $this->authorize('edit', $model);
        return $content
            ->title($this->title)
            ->description(trans('admin.edit'))
            ->body($this->form($model)->edit($id));
    }

    public function update($id)
    {
        $model = $this->repository->find($id);
        $this->authorize('update', $model);
        return $this->form()->update($id);
    }

    protected function form($model=null)
    {
        return $this->service->form($model);
    }
}
